#include<stdio.h>
#include<ctime>
#include<cstdlib>
#include"creature2.h"
#include "nature.h"

using namespace std;

Wolf::Wolf() {}

Wolf::Wolf(int name)
{
	// m_flag = { 1,0,1,0,0,20,100,200,1 };
	m_name = name;
	m_age = rand() % m_flag.age_up;/*年龄在上限内随机生成*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));/*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2 + (rand() % (m_flag.hunger_full / 2 + 1));/*饥饿值在饱一半到全饱内生成*/
	m_flag.gender = 1 + (rand() % 2);
	m_position = { rand() % 50,rand() % 50 };
}

Wolf::Wolf(int name, int x, int y, Flag flag)
{
	m_flag = flag;/*先把标签赋给新生物，确定种族和上限，其他的之后覆盖*/
	/*创建一个生物，随机生成数值,x和y坐标固定*/
	m_position.x = x;
	m_position.y = y;
	m_name = name;
	m_age = 1;/*新生物的年龄为1*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));/*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2;/*饥饿值为半饱*/
	m_flag.gender = 1 + (rand() % 2);
}

/*记录狩猎事件,只有狼有*/
void Wolf::record(Creature& c)
{
	nature.events[nature.temp_events] = new Events(nature.time_past, m_position, m_name, m_flag.race);
	nature.events[nature.temp_events]->get(c);
	nature.temp_events++;
	nature.num.events++;
}

void Wolf::choose_target_race(int mode)
{
	switch (mode)
	{
	case 1:
		m_target.race = nature.sheep;
		m_target.num = &nature.num.sheep;
		break;
	case 2:
		m_target.race = nature.wolf;
		m_target.num = &nature.num.wolf;
		break;
	default:
		break;
	}
}

Sheep::Sheep() {}

Sheep::Sheep(int name)
{
	// m_flag = { 2,0,1,0,0,15,100,200,1 };
	m_name = name;
	m_age = rand() % m_flag.age_up;/*年龄在上限内随机生成*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));/*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2 + (rand() % (m_flag.hunger_full / 2 + 1));/*饥饿值在饱一半到全饱内生成*/
	m_flag.gender = 1 + (rand() % 2);
	m_position = { rand() % 50,rand() % 50 };
}

Sheep::Sheep(int name, int x, int y, Flag flag)
{
	m_flag = flag;/*先把标签赋给新生物，确定种族和上限，其他的之后覆盖*/
	/*创建一个生物，随机生成数值,x和y坐标固定*/
	m_position.x = x;
	m_position.y = y;
	m_name = name;
	m_age = 1;/*新生物的年龄为1*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));/*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2;/*饥饿值为半饱*/
	m_flag.gender = 1 + (rand() % 2);
}

void Sheep::choose_target_race(int mode)
{
	switch (mode)
	{
	case 1:
		m_target.race = nature.grass;
		m_target.num = &nature.num.grass;
		break;
	case 2:
		m_target.race = nature.sheep;
		m_target.num = &nature.num.sheep;
		break;
	default:
		break;
	}
}

void Sheep::search()/*羊在搜索时固定优先搜索有没有狼在视野里，如果有则将行动模式直接改为3*/
{
	int flag = 0;
	for (int i = 1; i <= nature.num.wolf; i++)
	{
		if (nature.wolf[i]->m_position.x <= m_position.x + m_sight || nature.wolf[i]->m_position.x >= m_position.x - m_sight)
		{
			if (nature.wolf[i]->m_position.y <= m_position.y + m_sight || nature.wolf[i]->m_position.y >= m_position.y - m_sight)
			{
				flag = 1;
				get_distance(*nature.wolf[i]);
				/*如果视野里搜索到狼，测定距离并选择对象*/
			}
		}
	}
	if (flag == 1)
	{
		m_flag.mode = 3;
		return;
	}/*确定有狼则准备逃命，直接退出函数*/
	for (int i = 1; i <= *m_target.num; i++)
	{
		Creature* c = m_target.race[i];
		judge(*c);
	}
	if (m_target.self == nullptr)/*如果搜索一圈后没有结果，则行动模式改为闲逛*/
	{
		m_flag.mode = 0;
	}
}

Grass::Grass() {}

Grass::Grass(int name, int x, int y, Flag flag)
{
	m_flag = flag;/*先把标签赋给新生物，确定种族和上限，其他的之后覆盖*/
	/*创建一个生物，随机生成数值,x和y坐标固定*/
	m_position.x = x;
	m_position.y = y;
	m_name = name;
	m_age = 1;/*新生物的年龄为1*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));/*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2;/*饥饿值为半饱*/
	m_flag.gender = 1 + (rand() % 2);
}

Grass::Grass(int name)
{
	// m_flag = { 3,3,1,0,0,10,100,100,1 };
	m_name = name;
	m_age = rand() % m_flag.age_up;/*年龄在上限内随机生成*/
	m_hp = m_flag.hp_up / 2 + (rand() % (m_flag.hp_up / 2 + 1));/*生命值在上限一半到上限内生成*/
	m_hunger = m_flag.hunger_full / 2 + (rand() % (m_flag.hunger_full / 2 + 1));/*饥饿值在饱一半到全饱内生成*/
	m_flag.gender = 1 + (rand() % 2);
	m_position = { rand() % 50,rand() % 50 };
}

void Grass::action()/*草没有移动和视力，故只需要分配模式为进食（生长）和繁衍*/
{
	situation();
	switch (m_flag.mode)
	{
	case 1:
		eat()/*草单独调用*/;
		break;
	case 2:
		breed();/*草是往四周新建四个对象*/
		break;
	default:
		break;
	}
}
/*直接加40饥饿值*/
void Grass::eat()
{
	m_hunger += 40;
}
void Grass::breed()/*草是向上下左右四格生成子类*/
{
	m_hp -= 50;
	/*草是直接新建，所以新草的编号就是总数后4个，最后把总数再加4满足范围*/
	nature.grass[nature.num.grass += 1] = new Creature(nature.num.grass += 1, m_position.x++, m_position.y, nature.grass[m_name]->m_flag);
	nature.grass[nature.num.grass += 2] = new Creature(nature.num.grass += 2, m_position.x--, m_position.y, nature.grass[m_name]->m_flag);
	nature.grass[nature.num.grass += 3] = new Creature(nature.num.grass += 3, m_position.x, m_position.y++, nature.grass[m_name]->m_flag);
	nature.grass[nature.num.grass += 4] = new Creature(nature.num.grass += 4, m_position.x, m_position.y--, nature.grass[m_name]->m_flag);
	nature.num.grass += 4;

}
/*草没有记录，所以直接空函数*/
void Grass::record()
{

}